package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @author: luoqianjin
 * @date: 2019/7/30 15:44
 * @description: 預約设置
 */
@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * excel文件上传，并解析文件内容保存到数据库
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        //读取excel文件
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            ArrayList<OrderSetting> orderSettingList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(list)) {
                list.forEach(item -> {
                    OrderSetting orderSetting = new OrderSetting();
                    orderSetting.setOrderDate(new Date(item[0]));
                    orderSetting.setNumber(Integer.parseInt(item[1]));
                    orderSettingList.add(orderSetting);
                });
                orderSettingService.upload(orderSettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据日期查询预约设置数据（获取指定日期所在月份的预约设置数据）
     *
     * @param date
     * @return
     */
    @RequestMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//参数格式为：2019-03
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据指定日期修改可预订人数
     */
    @RequestMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);

    }
}
